package com.jeasonzhao.report.vml.chart.data;


public class ElementCollection
{
    private java.util.Vector m_vectData = null;
    private Integer m_InstanceLocker = new Integer(0);
    public ElementCollection()
    {
        super();
    }

    public static ElementCollection fromArray(Element[] arys)
    {
        if(null == arys)
        {
            return null;
        }
        ElementCollection ret = new ElementCollection();
        for(int n = 0;n < arys.length;n++)
        {
            ret.addElement(arys[n]);
        }
        return ret;
    }

    public static ElementCollection fromList(java.util.List list)
    {
        if(null == list)
        {
            return null;
        }
        ElementCollection ret = new ElementCollection();
        for(int n = 0;n < list.size();n++)
        {
            ret.addElement((Element) list.get(n)); //may cause a cast-exception.
        }
        return ret;
    }

    public void addAll(ElementCollection colls)
    {
        if(null == colls || colls.m_vectData == null)
        {
            return;
        }
        initVector();
        m_vectData.addAll(colls.m_vectData);

    }

    public void addAll(java.util.List colls)
    {
        if(null == colls)
        {
            return;
        }
        initVector();
        for(int n = 0;n < colls.size();n++)
        {
            this.add((Element) colls.get(n));
        }
    }

    public void addAll(Element[] colls)
    {
        if(null == colls)
        {
            return;
        }
        initVector();
        for(int n = 0;n < colls.length;n++)
        {
            this.add(colls[n]);
        }
    }

    private void initVector()
    {
        /**
         * Must using singleton initilizer-method at here to prevent re-writting vector
         */
        if(null == m_vectData)
        {
            synchronized(m_InstanceLocker)
            {
                if(null == m_vectData)
                {
                    m_vectData = new java.util.Vector();
                }
            }
        }
    }

    public void compress()
    {
        for(int n = 0;null != m_vectData && n <= m_vectData.size();n++)
        {
            if(m_vectData.elementAt(n) == null)
            {
                m_vectData.remove(n);
                n = n == 0 ? 0 : n - 1;
            }
        }
    }

    public Element elementAt(int nIndex)
    {
        return null == m_vectData ? null : (Element) m_vectData.elementAt(nIndex);
    }

    public Element get(int nIndex)
    {
        return elementAt(nIndex);
    }

    public java.util.Iterator iterator()
    {
        return null == m_vectData ? null : m_vectData.iterator();
    }

    public void addElement(Element value)
    {
        if(null == value)
        {
            return;
        }
        initVector();
        m_vectData.addElement(value);
    }

    public void add(Element value)
    {
        addElement(value);
    }

    public void insertElementAt(Element value,int nIndex)
    {
        initVector();
        m_vectData.insertElementAt(value,nIndex);
    }

    public void clear()
    {
        if(null != m_vectData)
        {
            m_vectData.clear();
        }
    }

    public void removeElementAt(int nIndex)
    {
        if(null == m_vectData || nIndex < 0 || nIndex >= m_vectData.size())
        {
            return;
        }
        m_vectData.removeElementAt(nIndex);
    }

    public void removeElement(Element item)
    {
        if(null == m_vectData)
        {
            return;
        }
        m_vectData.removeElement(item);
    }

    public void remove(Element item)
    {
        removeElement(item);
    }

    public void remove(int nIndex)
    {
        removeElementAt(nIndex);
    }

    public int indexOf(Element value)
    {
        if(null == m_vectData || null == value)
        {
            return -1;
        }
        return m_vectData.indexOf(value);
    }

    public int size()
    {
        return null == m_vectData ? 0 : m_vectData.size();
    }

    public Element[] toArray()
    {
        if(null == m_vectData)
        {
            return null;
        }
        Element[] ret = new Element[m_vectData.size()];
        m_vectData.copyInto(ret);
        return ret;
    }

    public void sort(java.util.Comparator comp)
    {
        if(null == m_vectData || null == comp || m_vectData.size() < 1)
        {
            return;
        }
        Element[] items = this.toArray();
        java.util.Arrays.sort(items,comp);
        m_vectData.clear();
        for(int n = 0;n < items.length;n++)
        {
            m_vectData.addElement(items[n]);
        }
    }

}
